<?php
/**
 * lertaLight - A smart ContentManagementSystem
 * --
 * 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * --
 *
 * @package 	lertaLight
 * @author 		Frederic Darmstädter <fredericdarmstaedter@gmail.com>
 * @copyright 	2010 Frederic Darmstädter <fredericdarmstaedter@gmail.com>
 * @license 	http://opensource.org/licenses/mit-license.php MIT License
 * @version 	2.0 (2010-01-16)
 */
	class MenuGenerator{

		public function getMenu($pageRequest, $routes){
			return $this->listWrapper($this->listPoint($this->getListPoints($pageRequest, $routes)));
		}
		
		protected function listWrapper($content){
			return '<ul class="menu">' . $content . '</ul>';
		}
		
		protected function listPoint($listPoints){
			$output = '';
			foreach($listPoints as $listPoint){
				$output .= '<li>' . $listPoint . '</li>';
			}
			return $output;
		}
		
		protected function getListPoints($pageRequest, $routes){
			$config = new config();
			$configuration = $config->config();
			$output = array();
			$i = 0;
			foreach($routes as $route){
				if(empty($route['notInMenu'])){
					if(empty($route['redirect'])){
						$key = array_keys($routes);
						$link = '<a ';
						if($key[$i] == $pageRequest){
							$link .= 'class="active" ';
						}
						$link .= 'href="';
						$linkPrepare = $configuration['baseUrl'];
						$linkPrepare .= $key[$i];
						$linkPrepare = str_replace('//', '/', $linkPrepare);
						$link .= str_replace('http:/', 'http://', $linkPrepare);
						
						if(empty($route['menutitle']))
							$title = $route['title'];
						else
							$title = $route['menutitle'];
						$link .= '">' . $title;
						$link .= '</a>';
						$output[$i] = $link;
					}
					
				}
				
				
				$i++;
				
			}
			return $output;
		}
	}